package com.tensquare.base.service;

import cn.hutool.core.util.StrUtil;
import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import com.tensquare.entity.IdWorker;
import com.tensquare.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by WF on 2020/12/14 15:32
 */
@Service
public class LabelService {

    @Autowired
    private LabelDao labelDao;
    @Autowired
    private IdWorker idWorker;


    //1.添加标签
    public void add(Label label) {
        //1.1)设置主键
        long id = idWorker.nextId();
        label.setId(id + "");
        //1.2)保存
        labelDao.save(label);
    }
    //2.查询所有标签
    public  List<Label> findAll() {
        return labelDao.findAll();
    }

    //3.查询单个标签
    public Label findById(String labelId) {
        return labelDao.findById(labelId).get();
    }

    //4.修改标签
    public void update(Label label) {
        labelDao.save(label);
    }

    //5.删除标签
    public void delete(String labelId) {
        labelDao.deleteById(labelId);
    }

    //6.进行条件查询
    public List<Label> search(Label label) {
        //6.1)进行条件查询
        Specification<Label> specification = createSpecification(label);
        //6.2)返回查询结果
        return labelDao.findAll(specification);
    }

    //7.进行条件查询
    private Specification<Label> createSpecification(Label label) {
        //HQL: Hibernate Query Language
        //select st.sid,st.sname,st.sex,st.addr from Student st; //类似的构造方法：(int sid,String sname,String sex,String addr)
        return new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //7.1)定义查询条件的集合
                List<Predicate> predicateList = new ArrayList<>();
                //7.2)判断传入的参数是否有值
                if(label != null){
                    if(StrUtil.isNotBlank(label.getLabelname())){       //标签名
                        predicateList.add(cb.like(root.get("labelname").as(String.class),"%" + label.getLabelname() + "%"));
                    }
                    if(StrUtil.isNotBlank(label.getState())){           //状态值
                        predicateList.add(cb.equal(root.get("state").as(String.class),label.getState()));
                    }
                    if(StrUtil.isNotBlank(label.getRecommend())){       //是否推荐
                        predicateList.add(cb.equal(root.get("recommend").as(String.class),label.getRecommend()));
                    }
                }
                //7.3)定义与条件集合大小一致的数组
                Predicate[] predicates = new Predicate[predicateList.size()];
                //7.4)将集合的内容放到数组中
                Predicate[] predicates1 = predicateList.toArray(predicates);
                //7.5)返回条件
                return cb.and(predicates1);
            }
        };
    }
    //8.分页带条件查询
    public PageResult<Label> search(Label label, Integer page, Integer size) {
        //8.1)进行分页查询
        Page<Label> labelPage = labelDao.findAll(createSpecification(label), PageRequest.of(page - 1, size));
        //8.2)返回分页结果
        return new PageResult<>(labelPage.getTotalElements(),labelPage.getContent());
    }
}


